package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(2)
@Aspect
@Component
public class SysTimeAspect {
    /**
     * 定义一个切入点
     */
    @Pointcut("bean(*ServiceImpl)")
    public void doTime(){}

    /**
     * 前置通知@Before通知在目标方法执行之前执行
     * @param
     */
    @Before("doTime()")
    public void dpBefore(JoinPoint jp){
        System.out.println("time doBefore()");
    }

    /**
     * @After通知 在目标方法结束（return或throw之前）之前执行
     */
    @After("doTime()")
    public void doAfter(){
        System.out.println("time doAfter()");
    }

    /**
     * AfterReturning通知
     * after之后程序正常返回，没有出现异常则执行此通知
     */
    @AfterReturning("doTime()")
    public void doAfterReturning(){
        System.out.println("time doAfterReturning");
    }

    /**
     * AfterThrowing通知
     * after之后程序出现异常则执行此通知
     */
    @AfterThrowing("doTime()")
    public void doAfterThrowable(){
        System.out.println("time doAfterThrowing");
    }

    /**
     * Around环绕异常
     *ProceedingJoinPoint这个类型只能做为环绕通知的方法参数
     * @param jp
     * @return
     * @throws Throwable
     */
    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint jp)throws Throwable{

        System.out.println("doAround.before");
        try{
            Object obj=jp.proceed();
            System.out.println("doAround.after");
            return obj;
        }catch (Throwable e){
            System.out.println(e.getMessage());
            throw e;//检查异常
        }
    }
}